package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.geom.ColorTransform;
	import ex.Circle;
	import Main;
	import Snapshot;
	/**
	 * ...
	 * @author Excel Excel
	 */
	public class MovingCircle extends Sprite
	{
		public static const GroupPlayer:int = 1;
		public static const GroupEnemy:int = 2;
		public static const GroupWall:int = 4;
		public static const GroupBullet:int = 8;
		public static const GroupWall2:int = 16;
		public static const GroupJunk:int = 32;
		//Animation magic
		public var sa:Snapshot;
		public var sb:Snapshot;
		
		
		
		
		public var dx:Number;
		public var dy:Number;
		public var r:Number;
		public var shield:int;
		public var armor:int;
		public var speed:Number;
		public var boost:Boolean;
		public var removeMe:Boolean;
		public var brainDead:Boolean;
		//Bitmap of groups the object collides with
		public var MaskCollide:int;
		//Bitmap of groups the object interacts with 
		public var MaskInteract:int;
		//Bitmap of groups the object belongs to
		public var MaskPublic:int;
		
		public var hitpoints:int;
		
		public function CollideableWith(obj:MovingCircle):Boolean
		{
			return (obj.MaskPublic & this.MaskCollide)
		}
		//virtual !?
		public function Interact(obj:MovingCircle):Boolean
		{
			if (!(obj.MaskPublic & this.MaskInteract))
			{
				return false;
			}
			//Possibly write something there
			return true;
		}
		
		protected function Draw():void
		{}
		
		public function destroy():void
		{}
		
		public function movePlayer(event:Event):void
		{
			///Override 
		}
		
		public function SetStartPosition(x_:Number, y_:Number)
		{
			x = sa.x = sb.x = x_;
			y = sa.y = sb.y = y_;
		}
		
		public function SaneStates():void//Check whether we're between sa and sb
		{
			if (x > sa.x && x > sb.x) throw("Insane state!");
			if (x < sa.x && x < sb.x) throw("Insane state!");
			if (y > sa.y && y > sb.y) throw("Insane state!");
			if (y < sa.y && y < sb.y) throw("Insane state!");
		}
		
		override public function set x (value:Number) : void
		{
			if (value > sa.x && value > sb.x) throw("Insane state!");
			super.x = value
		}
		
		override public function set y (value:Number) : void
		{
			if (value > sa.y && value > sb.y) throw("Insane state!");
			super.y = value
		}
		
		public function ColorMeIfImDead()
		{
			if (!IsAlive())
			{
				var color:ColorTransform = transform.colorTransform;
				color.alphaMultiplier = 0.3;
				transform.colorTransform = color;
				MaskPublic = 0;//No-one should collide with it anymore
			}
			ColorMePosessed();
		}
		
		public function ColorMePosessed()
		{
			if (brainDead)
			{
			var color:ColorTransform = transform.colorTransform;
			color.greenMultiplier = .5;
			color.blueMultiplier = .5;
			color.redMultiplier = .5;
			color.greenOffset = 127;
			color.redOffset = 127;
			color.blueOffset = 127;
			transform.colorTransform = color;
			}
		}
		
		public function IsPlayer():Boolean
		{
			if (this as Player)
				return true;
			else
				return false;
		}
		public function IsAlive():Boolean
		{
			return (hitpoints > 0);
		}
		public function Damage(dam:Number)
		{
			var a:Boolean = IsAlive();
			if (!armor)
				hitpoints -= Math.max(0.0, dam - shield);
			else
			{
				armor -= Math.max(0.0, dam - shield);
				if (armor < 0)
				{
					hitpoints += armor;
					armor = 0;
				}
			}
			if (a != IsAlive())
			{
				Draw();
				OnDie();
				Main.Instance().killFollower(this);
			}
		}
		public function OnDie():void
		{
			destroy();
		}
		public function GetCircleOld(old:Circle, t:Number):void
		{
			old.center.x = x + dx * t;
			old.center.y = y + dy * t;
			old.radius = r;
		}
		
		public function GetCircle(t:Number):Circle
		{
			return new Circle(x + dx * t, y + dy * t, r);
		}
		public function MovingCircle(a:Number, s:Number,mc:int = 55,mi:int = 0, mp:int = 2) 
		{
			super();
			brainDead = false;
			r = a;
			speed = s;
			dx = 0;
			dy = 0;
			shield = 0.0;
			armor = 0.0;
			MaskInteract = mi;
			MaskPublic = mp;
			MaskCollide = mc;
			removeMe = false;
			sa = new Snapshot(this);
			sb = new Snapshot(this);
		}

		public function get left () : Number
		{
			return x - r;
		}
		
		public function get right() : Number
		{
			return x + r;
		}
		
		public function get top() : Number
		{
			return y - r;
		}
		
		public function get bottom() : Number
		{
			return y + r;
		}
		public function moveForward():void
		{
			_moveForward();
		}
		
		final public function _moveForward():void
		{
			var angle:Number = 2 * Math.PI * rotation / 360;
			dx = (1+boost)*speed * Math.cos(angle);
			dy = (1+boost)*speed * Math.sin(angle);
	//		x += dx;
	//		y += dy;
		}
		
		static const WallFake:MovingCircle = new MovingCircle(0, 0,0,0,GroupWall);
		static const Wall2Fake:MovingCircle = new MovingCircle(0, 0,0,0,GroupWall2);
		
	}

}